import { defineStore } from 'pinia';

interface AppState {
  /** 侧边栏折叠状态 */
  sidebarCollapse: boolean;
  /** 主题色 */
  primaryColor: string;
  /** 暗黑模式 */
  isDark: boolean;
  /** 语言 */
  language: 'zh-CN' | 'en-US';
}

export const useAppStore = defineStore('app', {
  state: (): AppState => ({
    sidebarCollapse: false,
    primaryColor: '#409EFF',
    isDark: false,
    language: 'zh-CN'
  }),

  persist: {
    key: 'app-settings',
    storage: localStorage,
    paths: ['primaryColor', 'isDark', 'language']
  },

  actions: {
    // 切换侧边栏折叠状态
    toggleSidebar() {
      this.sidebarCollapse = !this.sidebarCollapse;
    },

    // 设置主题色
    setPrimaryColor(color: string) {
      this.primaryColor = color;
      // 更新CSS变量
      document.documentElement.style.setProperty('--el-color-primary', color);
    },

    // 切换暗黑模式
    toggleDark() {
      this.isDark = !this.isDark;
      if (this.isDark) {
        document.documentElement.classList.add('dark');
        document.documentElement.style.setProperty('--el-text-color-regular', '#303133');
      } else {
        document.documentElement.classList.remove('dark');
      }
    },

    // 设置语言
    setLanguage(lang: AppState['language']) {
      this.language = lang;
    }
  }
});
